package cn.xeblog.forward;

import cn.xeblog.forward.handler.ForwardInitializer;
import cn.xeblog.forward.utils.YamlUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;

/**
 * @author eleven
 * @date 2024/12/16 8:00
 * @apiNote
 */
@Slf4j
public class ForwardServer {

    public static void main(String[] args) {
        new ForwardServer().start();
    }

    private void start() {

        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();
        int port;
        try {
            port = Integer.parseInt(String.valueOf(YamlUtil.getValue("server.port")));
        } catch (Exception e) {
            port = 9528;
        }
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap()
                    .group(boss, worker)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ForwardInitializer());
            ChannelFuture future = serverBootstrap.bind(port).sync();
            future.channel().closeFuture().sync();
            log.info("Forward Server started");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }

    }
}
